<template>
  <j-modal
    title="关联分类"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    okText="保存"
    cancelText="关闭">
    <a-row>
      <a-col :span="24">
        <a-button @click="handleOpenCascade" type="primary">新增关联</a-button>
      </a-col>
      <a-col :span="24">
        <a-table
          size="middle"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :loading="loading"
        >
          <template slot="action" slot-scope="text, record">
            <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record)">
              <a>删除</a>
            </a-popconfirm>
          </template>
        </a-table>
      </a-col>
    </a-row>
    <cascade-modal ref="cascadeModal" @ok="handleOkCascade"></cascade-modal>
  </j-modal>
</template>
<script>

import {deleteAction, getAction, postAction} from "@api/manage";
import CascadeModal from "@/management/common_mng/associateCategory/modules/CascadeModal";

export default {
  name: 'AssociateCategory',
  components: {
    CascadeModal
  },
  data() {
    return {
      model: {},
      width: 450,
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '品牌名称',
          align: "center",
          dataIndex: 'brandName'
        },
        {
          title: '分类名称',
          align: "center",
          dataIndex: 'catelogName'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          fixed: "right",
          width: 87,
          scopedSlots: {customRender: 'action'}
        }
      ],
      dataSource: [],
      url: {
        list: "/category/pmsCategoryBrandRelation/queryCategoryListByBrandId",
        saveBatch: "/category/pmsCategoryBrandRelation/saveBatch",
        delete: "/category/pmsCategoryBrandRelation/delete",
      },
      loading: false,
      visible: false,
      brand: {}
    }
  },
  computed: {},
  methods: {
    handleOpenCascade() {
      console.log("handleOpenCascade")
      this.$refs.cascadeModal.show()
    },

    close() {
      this.$emit('close');
      this.visible = false;
    },

    handleOk() {
      this.handleSaveBatch();
    },

    submitCallback() {
      this.$emit('ok');
      this.visible = false;
    },

    handleCancel() {
      this.close()
    },

    add(record) {
      this.visible = true
      this.brand = record
      this.loadData()
    },

    // 保存关联数据
    handleSaveBatch() {
      if (!this.url.saveBatch) {
        this.$message.error("请设置url.saveBatch属性!")
        return
      }

      if (this.dataSource.length > 0) {
        postAction(this.url.saveBatch, this.dataSource).then(res => {
          if (res.success) {
            this.$message.success(res.message)
          } else {
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.visible = false
        })
      }
    },

    // 删除关联数据
    handleDelete(record) {
      if (record.hasOwnProperty("id")) {
        deleteAction(this.url.delete, {id: record.id}).then((res) => {
          if (res.success) {
            this.$message.success(res.message)
          } else {
            this.$message.warning(res.message)
          }
        })
      } else {
        this.$message.success("删除成功!")
      }
      this.dataSource = this.dataSource.filter(item => !(item.brandId === record.brandId && item.catelogId === record.catelogId))
    },

    // 用于回显关联数据
    async loadData() {
      if (!this.url.list) {
        this.$message.error("请设置url.list属性!")
        return
      }
      let params = {}
      this.loading = true
      params.brandId = this.brand.brandId
      getAction(this.url.list, params).then(res => {
        if (res.success) {
          this.dataSource = res.result
        }
      }).finally(() => {
        this.loading = false
      })
    },

    handleOkCascade(value, selectOptions) {
      let catelogId = selectOptions[selectOptions.length - 1].value
      // 判断是否有这条数据
      let data = this.dataSource.find(item => item.brandId === this.brand.brandId && item.catelogId === catelogId)

      if (!data) {
        this.dataSource.push({
          catelogName: selectOptions[selectOptions.length - 1].label,
          brandName: this.brand.name,
          catelogId: catelogId,
          brandId: this.brand.brandId
        })
      } else {
        this.$message.warn("该数据已在关联列表内")
      }
    }
  }
}
</script>

<style scoped>
</style>